﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using Mogre;
using MogreNewt;

namespace WastedMechanics
{
    class Level
    {
        Entity GraphicsEntity;
        SceneNode GraphicsNode;

        Entity CollisionEntity;
        SceneNode CollisionNode;

        Entity PlantsEntity;
        SceneNode PlantsNode;

        Body Body;

        List<ClimbEdge> ClimbingEdges;

        public NavMesh.Mesh Navmesh;

        public void SetGraphicsMesh(String meshFile)
        {
            GraphicsNode =
                Engine.SceneManager.RootSceneNode.CreateChildSceneNode();
            GraphicsEntity = Engine.SceneManager.CreateEntity(meshFile);
            GraphicsNode.AttachObject(GraphicsEntity);
            GraphicsEntity.CastShadows = true;
        }

        public void SetPlantsMesh(String meshFile)
        {
            PlantsNode =
                Engine.SceneManager.RootSceneNode.CreateChildSceneNode();
            PlantsEntity = Engine.SceneManager.CreateEntity(meshFile);
            PlantsNode.AttachObject(PlantsEntity);
            PlantsEntity.CastShadows = true;
        }

        public void SetCollisionMesh(String meshFile)
        {
            CollisionNode = 
                Engine.SceneManager.RootSceneNode.CreateChildSceneNode();
            CollisionEntity = Engine.SceneManager.CreateEntity(meshFile);
            CollisionNode.AttachObject(CollisionEntity);
 
            // Nie chcemy aby siatka kolizyjna była widoczna
            CollisionNode.SetVisible(false);

             MogreNewt.CollisionPrimitives.TreeCollisionSceneParser collision = 
                 new MogreNewt.CollisionPrimitives.TreeCollisionSceneParser(
                Engine.NewtonWorld);
            collision.ParseScene(CollisionNode, false, 1);
            Body = new Body(Engine.NewtonWorld, collision);
            collision.Dispose();
            Body.AttachNode(CollisionNode);
            Body.UserData = this;
            Body.MaterialGroupID = Engine.MaterialManager.Materials["Level"];
        }

        public void SetClimbingEdges(string file)
        {
            ClimbingEdges = new List<ClimbEdge>();
            using (BinaryReader reader = new BinaryReader(File.OpenRead(Engine.GetResourcePath(file))))
            {
                int count = reader.ReadInt32();
                for (int i = 0; i < count; i++)
                {
                    Vector3 a = new Vector3(); ;
                    a.x = reader.ReadSingle();
                    a.y = reader.ReadSingle();
                    a.z = reader.ReadSingle();
                    Vector3 b = new Vector3(); ;
                    b.x = reader.ReadSingle();
                    b.y = reader.ReadSingle();
                    b.z = reader.ReadSingle();                    
                    ClimbingEdges.Add(new ClimbEdge(a, b));
                }
            }
        }

        public void SetNavmesh(string file)
        {
            Navmesh = new NavMesh.Mesh();
            Navmesh.LoadFromFile(Engine.GetResourcePath(file));
        }
    }
}
